﻿using UnityEngine;
using System.Collections;

public class ServerClient : MonoBehaviour {

	string IP = "127.0.0.1";  
	int Port = 10100;  



	// Use this for initialization  
	void Start () {   
	}  

	//OnGUI方法，所有GUI的绘制都需要在这个方法中实现  
	void OnGUI(){  
		//Network.peerType是端类型的状态:  
		//即disconnected, connecting, server 或 client四种  
		switch(Network.peerType){
		//禁止客户端连接运行, 服务器未初始化  
		case NetworkPeerType.Disconnected:  
			StartServer();  
			break;  
			//运行于服务器端  
		case NetworkPeerType.Server:  
			OnServer();  
			break;  
			//运行于客户端  
		case NetworkPeerType.Client:  
			break;  
			//正在尝试连接到服务器  
		case NetworkPeerType.Connecting:  
			break;  
		}  
	}  

	void StartServer(){  
		GUILayout.Label("同步测试:");  
		//当用户点击按钮的时候为true  
		if (GUILayout.Button("创建服务器")) {  
			//初始化本机服务器端口，第一个参数就是本机接收多少连接  
			NetworkConnectionError error = Network.InitializeServer(12,Port,false);  
			//连接状态  
			switch(error){  
			case NetworkConnectionError.NoError:  
				break;  
			default:  
				Debug.Log("服务端错误"+error);  
				break;  
			}  
		}  
		if (GUILayout.Button("连接服务器")){  
			NetworkConnectionError error = Network.Connect(IP,Port);  
			//连接状态  
			switch(error){  
			case NetworkConnectionError.NoError:  
				break;  
			default:  
				Debug.Log("客户端错误"+error); 
				GUILayout.Label("客户端错误"+error.ToString());  
				break;  
			}  
		}  
	}  

	void OnServer(){  
		GUILayout.Label("服务端已经运行,等待客户端连接");  
		//Network.connections是所有连接的玩家, 数组[]  
		//取客户端连接数.   
		int length = Network.connections.Length;  
		//按数组下标输出每个客户端的IP,Port  
		for (int i=0; i<length; i++)  
		{  
			GUILayout.Label("客户端"+i);  
			GUILayout.Label("客户端ip"+Network.connections[i].ipAddress);  
			GUILayout.Label("客户端端口"+Network.connections[i].port);  
			GUILayout.Label("-------------------------------");  
		}  
		//当用户点击按钮的时候为true  
		if (GUILayout.Button("断开服务器")){  
			Network.Disconnect();  
		}  
	}  
	// 服务器断触发函数 当一个客户端从服务器端断开时触发
	void OnPlayerDisconnected(NetworkPlayer player) {
		//                                          玩家ip地址             玩家端口
		Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port);
		Network.RemoveRPCs(player);
		Network.DestroyPlayerObjects(player);

	}

	// Update is called once per frame  
	void Update ()   
	{   
	}  
}  